﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebMobie.Models;
using WebMobie.Models.Entities;
using WebMobie.Modules.Role;

namespace WebMobie.Controllers
{
    public class FeedbackController : ApiController
    {
        static readonly WebMobileContext db = new WebMobileContext();
        //
        // GET: /Feedback/

        public IEnumerable<FeedBack> GetAllFeedbackOfProduct(int mProductId)
        {
            var feedbacks = (from feedback in db.GetAllFeedback() where feedback.Product.ProductId == mProductId select feedback).ToList();
            return feedbacks;
        }



        public HttpResponseMessage PostFeedBack(FeedBack mFeedback)
        {
            
            
            if (ModelState.IsValid)
            {
                // check modify Role
                var productList = db.GetAllProduct().ToList();
                var product = (from p in db.GetAllProduct() where p.ProductId == mFeedback.ProductID select p).FirstOrDefault();
                mFeedback.Product = product;
                mFeedback.DateCreate = DateTime.Now;
                db.Feedbacks.Add(mFeedback);
                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK, mFeedback);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

       



        



    }
}
